package org.blackcat.web;

import org.blackcat.core.annotation.Login;
import org.blackcat.domain.ECG;
import org.blackcat.domain.common.MsgResponse;
import org.blackcat.domain.enums.UserEnum;
import org.blackcat.service.ECGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 詹泽
 * @date 2017/2/27
 */
@RestController
@RequestMapping("/ecg")
public class ECGController {

    @Autowired
    private ECGService ecgService;

    @RequestMapping(value = "/time",method = RequestMethod.GET)
    @Login
    public MsgResponse<ECG> getECGByTime(HttpServletRequest request, String time) throws Exception {
        String token = (String) request.getAttribute(UserEnum.USER_REQUEST_TOKEN.getValue());
        return ecgService.getECGByTime(token,time);
    }

    @RequestMapping(value = "/count",method = RequestMethod.GET)
    @Login
    public MsgResponse<Object> getECGTotalCount(HttpServletRequest request) throws Exception {
        String token = (String) request.getAttribute(UserEnum.USER_REQUEST_TOKEN.getValue());
        return ecgService.getECGTotalCount(token);
    }

    @RequestMapping(value = "/{pageIndex}/{pageCount}")
    @Login
    public MsgResponse<List<ECG>> listECG(HttpServletRequest request, @PathVariable int pageIndex,@PathVariable int pageCount) throws Exception {
        String token = (String) request.getAttribute(UserEnum.USER_REQUEST_TOKEN.getValue());
        return ecgService.listECG(token,pageIndex,pageCount);
    };

    @RequestMapping(method = RequestMethod.PUT)
    @Login
    public MsgResponse<Object> addECG(ECG ecg, HttpServletRequest request) throws Exception {
        String token = (String) request.getAttribute(UserEnum.USER_REQUEST_TOKEN.getValue());
        return ecgService.addECG(ecg,token);
    }
}
